package chan.module.project.comm;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;


@Getter
@Setter
@ToString
public class FieldMeta {
    private String originalColumnName;       // 原列名‌:ml-citation{ref="1,4" data="citationList"}
    private String columnName;       // 列名‌:ml-citation{ref="1,4" data="citationList"}
    private String columnLabel;      // 列别名‌:ml-citation{ref="1" data="citationList"}
    private String javaType;         // Java 类型全名（如 java.lang.String）‌:ml-citation{ref="1,3" data="citationList"}
    private int sqlType;             // SQL 类型编号（如 Types.VARCHAR）‌:ml-citation{ref="1,3" data="citationList"}
    private String sqlTypeName;      // SQL 类型名称（如 VARCHAR）‌:ml-citation{ref="3,4" data="citationList"}
    private boolean isNullable;      // 是否允许空值‌:ml-citation{ref="1,5" data="citationList"}
    private int columnSize;          // 列大小（长度）‌:ml-citation{ref="1,3" data="citationList"}
    private int precision;           // 数值类型精度‌:ml-citation{ref="3" data="citationList"}
    private int scale;               // 数值类型小数位数‌:ml-citation{ref="3" data="citationList"}
    private boolean isAutoIncrement; // 是否自增‌:ml-citation{ref="1" data="citationList"}
    private boolean isPrimaryKey;    // 是否主键（需结合其他元数据获取）‌:ml-citation{ref="2,5" data="citationList"}
    private int columnIndex;
    public FieldMeta(ResultSetMetaData metaData, int columnIndex) throws SQLException {
        this.columnIndex = columnIndex;
        this.originalColumnName = metaData.getColumnName(columnIndex);
        this.columnName = this.originalColumnName;
        this.columnLabel = metaData.getColumnLabel(columnIndex);
        this.javaType = metaData.getColumnClassName(columnIndex);
        this.sqlType = metaData.getColumnType(columnIndex);
        this.sqlTypeName = metaData.getColumnTypeName(columnIndex);
        this.isNullable = metaData.isNullable(columnIndex) == ResultSetMetaData.columnNullable;
        this.columnSize = metaData.getColumnDisplaySize(columnIndex);
        this.precision = metaData.getPrecision(columnIndex);
        this.scale = metaData.getScale(columnIndex);
        this.isAutoIncrement = metaData.isAutoIncrement(columnIndex);
        this.isPrimaryKey = false;  // 需通过 DatabaseMetaData 额外判断‌:ml-citation{ref="2,5" data="citationList"}
    }

}
